package com.matt.components.aop;

import com.matt.domain.TeachOperationLog;
import com.matt.foundation.utils.ModelUtil;
import com.matt.foundation.utils.annotation.OptsTrace;
import com.matt.service.TeachOperationLogService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;


/**
 * Created by penghongqin on 14-8-31.
 */
@Component
@Aspect
@Slf4j
public class TeachOperationLogAop {

    @Autowired
    private TeachOperationLogService teachOperationLogService;

    @Pointcut("execution(* com.matt.service.impl.*ServiceImpl.*(..)) ")
    public void executeService() {

    }

    @Around("executeService()  && @annotation(optsTrace)")
    public Object aroundTest(ProceedingJoinPoint joinPoint, OptsTrace optsTrace) throws Throwable {
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes) ra;
        HttpServletRequest request = sra.getRequest();
        Object[] args = joinPoint.getArgs();

        matchSave(request, optsTrace.code(), optsTrace.params(), args);
        return joinPoint.proceed();
    }

    public TeachOperationLog matchSave(HttpServletRequest request, String code, String[] params, Object[] args) {
        switch (code) {
            case "0001":
                return teachOperationLogService.gardenAdviseLog(ModelUtil.getSafeStr(args[0]), ModelUtil.getSafeStr(args[1]), request.getHeader("ticket"), code);
            case "0005":
                return teachOperationLogService.addBirthdayRecordLog(request.getHeader("ticket"), code);
            case "0100":
                return teachOperationLogService.dropOutLog(ModelUtil.getSafeStr(args[0]), ModelUtil.getSafeStr(args[1]), ModelUtil.getSafeStr(args[2]), code);
            case "0101":
                return teachOperationLogService.graduateLog(ModelUtil.getSafeStr(args[0]), ModelUtil.getSafeStr(args[1]), ModelUtil.getSafeStr(args[2]), code);
            case "0300":
                return teachOperationLogService.addEvaluatingMLog(ModelUtil.getSafeStr(args[0]), ModelUtil.getSafeStr(args[1]), ModelUtil.getSafeStr(args[2]), code);
            case "0301":
                return teachOperationLogService.evaluatingMLog(ModelUtil.getSafeStr(args[0]), ModelUtil.getSafeStr(args[1]), ModelUtil.getSafeStr(args[2]), code);
            case "0302":
                return teachOperationLogService.addEvaluatingCBLog(ModelUtil.getSafeStr(args[0]), ModelUtil.getSafeStr(args[1]), ModelUtil.getSafeStr(args[2]), ModelUtil.getSafeStr(args[5]), code);
            case "0303":
                return teachOperationLogService.evaluatingCBLog(request.getHeader("ticket"), ModelUtil.getSafeStr(args[1]), code);
            case "0304":
                return teachOperationLogService.bindEvaluatingCBLog(request.getHeader("ticket"), ModelUtil.getSafeStr(args[1]), code);
            case "0305":
                return teachOperationLogService.addEvaluatingCELog(ModelUtil.getSafeStr(args[0]), ModelUtil.getSafeStr(args[1]), ModelUtil.getSafeStr(args[2]), ModelUtil.getSafeStr(args[3]), code);
            case "0306":
                return teachOperationLogService.evaluateEarlyMLog(request.getHeader("ticket"), ModelUtil.getSafeStr(args[1]), code);
            case "0307":
                return teachOperationLogService.addHealthInfo(request.getHeader("ticket"), ModelUtil.getSafeStr(args[1]), code);
            case "0308":
                return teachOperationLogService.modifyHealthInfo(request.getHeader("ticket"), ModelUtil.getSafeStr(args[1]), code);
            case "0310":
                return teachOperationLogService.addEvaluateEarlyMLog(request.getHeader("ticket"), ModelUtil.getSafeStr(args[2]), code);
            case "0311":
                return teachOperationLogService.addEvaluateEarlyMLog(request.getHeader("ticket"), ModelUtil.getSafeStr(args[2]), code);
            case "0400":
                return teachOperationLogService.addClassesLog(request.getHeader("ticket"), ModelUtil.getSafeStr(args[1]), code);
            case "0401":
                return teachOperationLogService.modifyClassesLog(request.getHeader("ticket"), ModelUtil.getSafeStr(args[1]), code);
            case "0402":
                return teachOperationLogService.classesPlanLog(request.getHeader("ticket"), ModelUtil.getSafeStr(args[0]), ModelUtil.getSafeStr(args[1]), code);
            case "0403":
                return teachOperationLogService.classesPrepareLog(request.getHeader("ticket"), ModelUtil.getSafeStr(args[3]), ModelUtil.getSafeStr(args[1]), ModelUtil.getSafeStr(args[2]), code);
            case "0440":
                return teachOperationLogService.checkInLog(request.getHeader("ticket"), ModelUtil.getSafeStr(args[3]), ModelUtil.getDefaultInteger(args[2]), ModelUtil.getDefaultInteger(args[8]), ModelUtil.getSafeStr(args[6]), code);
            case "0441":
                return teachOperationLogService.mulitCheckInLog(request.getHeader("ticket"), (String[]) args[1],code);
            case "0450":
                return teachOperationLogService.addObservationLog(request.getHeader("ticket"), ModelUtil.getSafeStr(args[3]), code);
            case "0451":
                return teachOperationLogService.addLiveRecordLog(request.getHeader("ticket"), ModelUtil.getDefaultInteger(args[4]), (String[]) args[3], code);
            case "0501":
                return teachOperationLogService.modifyChildrenCourseLog(request.getHeader("ticket"), ModelUtil.getSafeStr(args[1]), (String[]) args[0], code);
            case "0502":
                return teachOperationLogService.modifyCourseTimeLog(request.getHeader("ticket"), ModelUtil.getSafeStr(args[1]), ModelUtil.getSafeStr(args[2]), code);
            case "0503":
                return teachOperationLogService.earlyPrepareLog(request.getHeader("ticket"), ModelUtil.getSafeStr(args[1]), ModelUtil.getSafeStr(args[2]), code);
            case "0540":
                return teachOperationLogService.satCheckInLog(request.getHeader("ticket"), ModelUtil.getSafeStr(args[1]), ModelUtil.getDefaultInteger(args[2]), ModelUtil.getSafeStr(args[3]), code);
            case "0600":
                return teachOperationLogService.modifyLiveRoomLog(request.getHeader("ticket"), ModelUtil.getSafeStr(args[1]), code);
            case "0601":
                return teachOperationLogService.addTeachingAreaLog(request.getHeader("ticket"), ModelUtil.getSafeStr(args[0]), code);
            case "0602":
                return teachOperationLogService.modifyTeachingAreaLog(request.getHeader("ticket"), ModelUtil.getSafeStr(args[1]), code);
            case "0901":
                return teachOperationLogService.addStaffLog(request.getHeader("ticket"), ModelUtil.getSafeStr(args[0]), code);
            case "0902":
                return teachOperationLogService.modifyStaffLog(request.getHeader("ticket"), ModelUtil.getSafeStr(args[0]), code);
//            case "0903":
//                return teachOperationLogService.modifyStaffRoleLog(request.getHeader("ticket"), ModelUtil.getSafeStr(args[0]), ModelUtil.getSafeStr(args[1]), value);
            case "0905":
                return teachOperationLogService.dimissionLog(request.getHeader("ticket"), ModelUtil.getSafeStr(args[0]), code);
            default:
                return null;
        }
    }
}
